package com.cgbl.cg.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityMainBinding
import com.cgbl.cg.ui.main.ClassificationFragment
import com.cgbl.cg.ui.main.HomeFragment
import com.cgbl.cg.ui.main.MessageFragment
import com.cgbl.cg.ui.mine.MineFragment
import com.cgbl.cg.ui.vip.VipFragment
import com.cgbl.cg.vm.MainViewModel
import com.gyf.immersionbar.ktx.immersionBar
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.view.miuibottomnavigation.Mode
import com.robot.base.view.miuibottomnavigation.build.NavigationBuild
import me.goldze.mvvmhabit.BR

@Route(path = AppConstants.Router.Main.A_MAIN)
class MainActivity : GLBaseActivity<AppActivityMainBinding, MainViewModel>() {
    private var fragmentList = mutableListOf<Fragment>()
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_main

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        immersionBar {
            transparentStatusBar()
            keyboardEnable(false)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }
//        val navHostFragment =
//            supportFragmentManager.findFragmentById(R.id.fragments) as NavHostFragment
//        val navController = navHostFragment.navController;
//        binding.bottomNavigationView.itemIconTintList = null
//        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController);
        fragmentList.add(HomeFragment())
        fragmentList.add(ClassificationFragment())
        fragmentList.add(VipFragment())
        fragmentList.add(MessageFragment())
        fragmentList.add(MineFragment())
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
//        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.bottomNavigationView.init(
            NavigationBuild.Builder(this)
                .addItem(
                    getString(R.string.app_main_menu_0),
                    R.mipmap.app_ic_main_tab_1_un,
                    R.mipmap.app_ic_main_tab_1
                )
                .addItem(
                    getString(R.string.app_main_menu_1),
                    R.mipmap.app_ic_main_tab_2_un,
                    R.mipmap.app_ic_main_tab_2
                )
                .addItem(
                    getString(R.string.app_main_menu_2),
                    R.mipmap.app_ic_main_tab_3_un,
                    R.mipmap.app_ic_main_tab_3
                )
                .addItem(
                    getString(R.string.app_main_menu_3),
                    R.mipmap.app_ic_main_tab_4_un,
                    R.mipmap.app_ic_main_tab_4
                )
                .addItem(
                    getString(R.string.app_main_menu_4),
                    R.mipmap.app_ic_main_tab_5_un,
                    R.mipmap.app_ic_main_tab_5
                )
                .setMode(Mode.MODE_SCROLL)
                .setupWithViewPager(binding.mViewPager2)
                .setSelectTextColor(Color.RED)
                .setFixedItems(mutableSetOf(1))
                .build()
        )
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        val intent = Intent()
        // 为Intent设置Action、Category属性
        intent.action = Intent.ACTION_MAIN // "android.intent.action.MAIN"
        intent.addCategory(Intent.CATEGORY_HOME) //"android.intent.category.HOME"
        startActivity(intent)
    }
}